package appreate.sixnations.quiz;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import appreate.sixnations.quiz.R;
import appreate.sixnations.quiz.utils.CustomTypefaceSpan;

public class MainActivity extends Activity {

	private int selItem;
	private static Typeface mTypeface;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mTypeface = Typeface.createFromAsset(getAssets(), "fonts/wanted.ttf");

		((TextView) findViewById(R.id.quiz)).setTypeface(mTypeface);
		((TextView) findViewById(R.id.flags)).setTypeface(mTypeface);

		checkFirstLogin();

	}

	private void viewCategory(final Class<?> activity) {

		String title = getString(R.string.title_dialog_difficult);
		SpannableStringBuilder ssBuilser = new SpannableStringBuilder(title);
		CustomTypefaceSpan spanCustom = new CustomTypefaceSpan(Typeface.MONOSPACE.toString(), mTypeface);
		ssBuilser.setSpan(spanCustom, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

		final Dialog viewDialog = new Dialog(this);
		viewDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		viewDialog.setTitle(ssBuilser);
		LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View dialogView = li.inflate(R.layout.dialog_spinner, null);
		viewDialog.setContentView(dialogView);
		viewDialog.show();
		Spinner spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
		String[] array = getResources().getStringArray(R.array.difficult);
		CustomArrayAdapter<CharSequence> adapter = new CustomArrayAdapter<CharSequence>(this, array);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnercategory.setAdapter(adapter);

		spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
				selItem = arg2;
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		Button buttonOk = (Button) dialogView.findViewById(R.id.button_ok);
		buttonOk.setTypeface(mTypeface);
		// buttonOk.setTextSize(18);
		buttonOk.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Intent intent = new Intent(MainActivity.this, activity);
				intent.putExtra("mode", selItem);
				startActivity(intent);
				viewDialog.dismiss();
			}

		});

		Button buttonCancel = (Button) dialogView.findViewById(R.id.button_cancel);
		buttonCancel.setTypeface(mTypeface);
		// buttonCancel.setTextSize(18);
		buttonCancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				viewDialog.dismiss();
			}

		});

	}

	static class CustomArrayAdapter<T> extends ArrayAdapter<T> {
		public CustomArrayAdapter(Context ctx, T[] objects) {
			super(ctx, android.R.layout.simple_list_item_1, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = super.getView(position, convertView, parent);

			TextView text = (TextView) v;
			text.setTypeface(mTypeface);
			text.setGravity(Gravity.CENTER);
			text.setTextColor(Color.BLACK);

			return v;
		}

		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);

			TextView text = (TextView) view.findViewById(android.R.id.text1);
			text.setGravity(Gravity.CENTER);
			text.setTextColor(Color.BLACK);
			text.setTypeface(mTypeface);

			return view;

		}
	}

	private void checkFirstLogin() {

		int count = 0;

		SharedPreferences settings = getSharedPreferences("giro", Activity.MODE_PRIVATE);
		if (settings != null) {
			count = settings.getInt("numLogin", 0);
			if (count % 4 == 1) {
				new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.warning)
						.setMessage(R.string.msg_warning)
						.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog, int which) {
								startQuiz();
							}

						}).show();

			} else {
				startQuiz();
			}
			SharedPreferences.Editor editor = settings.edit();

			count++;
			editor.putInt("numLogin", count);
			editor.commit();

		}

	}

	private void startQuiz() {

		Button start = (Button) findViewById(R.id.start);
		start.setTypeface(mTypeface);
		LinearLayout startLayout = (LinearLayout) findViewById(R.id.startLayout);

		OnClickListener listenerNameToFlag = new OnClickListener() {

			@Override
			public void onClick(View v) {

				viewCategory(QuizNameToLogo.class);
			}
		};

		start.setOnClickListener(listenerNameToFlag);
		startLayout.setOnClickListener(listenerNameToFlag);

		Button start2 = (Button) findViewById(R.id.start2);
		start2.setTypeface(mTypeface);
		LinearLayout startLayout2 = (LinearLayout) findViewById(R.id.startLayout2);

		OnClickListener listenerFlagToName = new OnClickListener() {

			@Override
			public void onClick(View v) {

				viewCategory(QuizLogoToName.class);
			}
		};
		start2.setOnClickListener(listenerFlagToName);
		startLayout2.setOnClickListener(listenerFlagToName);

		Button start3 = (Button) findViewById(R.id.start3);
		start3.setTypeface(mTypeface);
		LinearLayout startLayout3 = (LinearLayout) findViewById(R.id.startLayout3);

		OnClickListener listenerSurvival = new OnClickListener() {

			@Override
			public void onClick(View v) {

				viewCategory(QuizSurvival.class);
			}
		};
		start3.setOnClickListener(listenerSurvival);
		startLayout3.setOnClickListener(listenerSurvival);

		Button record = (Button) findViewById(R.id.record);
		record.setTypeface(mTypeface);
		LinearLayout recordLayout = (LinearLayout) findViewById(R.id.recordLayout);

		OnClickListener listenerRecord = new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(MainActivity.this, Record.class);
				startActivity(intent);

			}
		};

		record.setOnClickListener(listenerRecord);
		recordLayout.setOnClickListener(listenerRecord);
	}
}